package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.domain.FlowDomain;
import com.dayuanit.dy15.atm.dto.FlowDTO;
import com.dayuanit.dy15.atm.entity.Flow;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.holder.PageHolder;
import com.dayuanit.dy15.atm.mapper.FlowMapper;
import com.dayuanit.dy15.atm.service.FlowService;
import com.dayuanit.dy15.atm.util.CardUtil;
import com.dayuanit.dy15.atm.util.DBUtils;
import com.dayuanit.dy15.atm.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class FlowServiceImpl implements FlowService {
    @Override
    public PageHolder<List<FlowDTO>> queryFLows(Long cardId, int currentPage) {
        if (null == cardId) {
            throw new BizException("sx, 错啦");
        }

        try (SqlSession sqlSession = DBUtils.getSqlSession()) {
            FlowMapper flowMapper = sqlSession.getMapper(FlowMapper.class);

            //总条数
            int total = flowMapper.countFlow(cardId);

            PageHolder<List<FlowDTO>> pageHolder = PageHolder.init(currentPage, total);

            //每页的数据
            List<FlowDomain> flows = flowMapper.listFlow(cardId, pageHolder.offset(), pageHolder.getPageSize());
            List<FlowDTO> flowDTOS = new ArrayList<>(flows.size());//防止数组扩容，优秀 推荐。
            for (FlowDomain flowDomain : flows) {
                FlowDTO flowDTO = new FlowDTO();
                //在set前或者后add都没关系，因为放到集合里的是内存地址。
                flowDTOS.add(flowDTO);

                flowDTO.setAmount(String.valueOf(flowDomain.getAmount() / 100));
                flowDTO.setCardNum(CardUtil.formatNum(flowDomain.getCardNum()));
                flowDTO.setCreateTime(DateUtil.date2String(flowDomain.getCreateTime()));
                flowDTO.setRemark(flowDomain.getRemark());
            }

            //每页的数据
            pageHolder.setData(flowDTOS);

            return pageHolder;
        }

    }

    @Override
    public List<FlowDTO> top10(long userId) {
        try (SqlSession sqlSession = DBUtils.getSqlSession()) {
            FlowMapper flowMapper = sqlSession.getMapper(FlowMapper.class);
            //TODO 用户先写死
            List<FlowDomain> flows = flowMapper.top10Flow(userId);
//            List<FlowDTO> dtos = new ArrayList();//普通程序员

            List<FlowDTO> dtos = new ArrayList(flows.size());//优秀程序员；当知道集合要放多少元素是，就给定容量；防止数组扩容

            for (FlowDomain flowDomain :  flows) {
                FlowDTO flowDTO = new FlowDTO();
                //在set前或者后add都没关系，因为放到集合里的是内存地址。
                dtos.add(flowDTO);

                flowDTO.setAmount(String.valueOf(flowDomain.getAmount() / 100));
                flowDTO.setCardNum(CardUtil.formatNum(flowDomain.getCardNum()));
                flowDTO.setCreateTime(DateUtil.date2String(flowDomain.getCreateTime()));
                flowDTO.setRemark(flowDomain.getRemark());
            }

            return dtos;
        }
    }
}
